import { Injectable, Version } from "@angular/core";
import { Observable, Subject } from "rxjs";
import { HttpClient, HttpHeaders } from "@angular/common/http";
import { FileUploader } from "ng2-file-upload";

@Injectable({
  providedIn: "root"
})
export class FileService {
  constructor(private http: HttpClient) {}
  projectName: string;
  string;
  version: string;
  httpOptions = {
    headers: new HttpHeaders({ "Content-Type": "application/json" })
  };
  // textHttpOptions: FileService["httpOptions"]= {
  //   headers: new HttpHeaders({ 'Content-Type': 'application/json' }),
  //     responseType: 'json'
  // };

  private newProEmit = new Subject<any>();
  newProEmmited$ = this.newProEmit.asObservable();
  newProject(isnew: boolean) {
    this.newProEmit.next(isnew);
  }

  private SafeNLEmit = new Subject<any>();
  SafeNLEmmited$ = this.SafeNLEmit.asObservable();
  setSafeNLName(SafeNLName: string) {
    this.SafeNLEmit.next(SafeNLName);
  }

  setProject(userName: string, projectName: string): Observable<any> {
    this.projectName = projectName;
    const url = `http://47.52.116.116:8075/file/setProject/?userName=${userName}&projectName=${projectName}`;
    //console.log(url);
    var res = this.http.get<boolean>(url);
    return res;
  }

  //上传state文件
  uploadFile(uploader: FileUploader, userName: string, projectName: string) {
    // 开始上传
    let url = `http://47.52.116.116:8075/file/upload?userName=${userName}&projectName=${projectName}`;
    uploader.setOptions({ url: url });
    uploader.uploadAll();
    this.getVersion(userName, this.projectName);
  }

  //上传state文件
  uploadState(uploader: FileUploader, userName: string, projectName: string, version: string) {
    // 开始上传
    let url = `http://47.52.116.116:8075/file/uploadState?userName=${userName}&projectName=${projectName}&version=${version}`;
    uploader.setOptions({ url: url });
    uploader.uploadAll();
    this.getVersion(userName, this.projectName);
  }

  //保存SafeNL文件
  getVersion(userName: string, projectName: string){
    const url = `http://47.52.116.116:8075/file/getVersion?userName=${userName}&projectName=${projectName}`;
    var version = this.http.get<any>(url);
    console.log(version)
  }

  //读取项目信息
  // getProject(projectAddress: string, ver): Observable<Project> {
  //   const url = `http://localhost8/file/getProject/${projectAddress}/${ver}`;
  //   let res = this.http.get<Project>(url);
  //   //console.log("begin");
  //   //console.log(url);
  //   //console.log("testttt");
  //   return res;
  // }
  // //读取pf，若文件不存在，则返回空串
  // getPf(projectAddress: string,ver):Observable<string> {
  //     const url = `http://localhost8/file/getPf/${projectAddress}/${ver}`;
  //     let res = this.http.get(url, {responseType: 'text'});
  //     //console.log("begin");
  //     //console.log(url);
  //     //console.log("testttt");
  //     return res;
  // }
  //读取pf,若文件不存在，则将项目转为pf文件，再将内容返给客户端
  // getNotNullPf(projectAddress: string, ver): Observable<string> {
  //   const url = `http://localhost8/file/getNotNullPf/${projectAddress}/${ver}`;
  //   let res = this.http.get(url, { responseType: "text" });
  //   //console.log("begin");
  //   //console.log(url);
  //   //console.log("testttt");
  //   return res;
  // }
  //将projectAddress对应的项目转为pf文件
  // ProjectToPf(projectAddress: string, project: Project): Observable<boolean> {
  //   const url = `http://localhost8/file/ProjectToPf/${projectAddress}`;
  //   var res = this.http.post<boolean>(url, project, this.httpOptions);
  //   return res;
  // }
  //读取项目信息
  // getLatestProject(projectAddress: string, ver): Observable<Project> {
  //   const url = `http://localhost8/file/getLatestProject/${projectAddress}/${ver}`;
  //   let res = this.http.get<Project>(url);
  //   //console.log("begin");
  //   //console.log(url);
  //   //console.log("testttt");
  //   return res;
  // }

  //保存SafeNL文件
  saveSafeNL(userName: string, projectName: string, version: string, SafeNL: string): Observable<string[]> {
    const url = `http://47.52.116.116:8075/file/saveSafeNL?userName=${userName}&projectName=${projectName}&version=${version}`;
    var res = this.http.post<string[]>(url, SafeNL, this.httpOptions);
    return res;
  }

  // format(projectAddress: string, project: Project): Observable<boolean> {
  //   const url = `http://localhost8/file/format/${projectAddress}`;
  //   var res = this.http.post<boolean>(url, project, this.httpOptions);
  //   return res;
  // }

  /*downloadProject(projectAddress: string) {
    var
      fs = require('fs'),
      url = require('url'),
      path = require('path'),
      http = require('http');

    // 从命令行参数获取root目录，默认是当前目录:
    var root = projectAddress + "\\Project.xml";


    // 创建服务器:
    var server = http.createServer(function (request, response) {
      // 获得URL的path，类似 '/css/bootstrap.css':
      var pathname = url.parse(request.url).pathname;
      // 获得对应的本地文件路径，类似 '/srv/www/css/bootstrap.css':
      var filepath = path.join(root, pathname);
      // 获取文件状态:
      fs.stat(filepath, function (err, stats) {
        if (!err && stats.isFile()) {
          // 没有出错并且文件存在:
          //console.log('200 ' + request.url);
          // 发送200响应:
          response.writeHead(200);
          // 将文件流导向response:
          fs.createReadStream(filepath).pipe(response);
        } else {
          // 出错了或者文件不存在:
          //console.log('404 ' + request.url);
          // 发送404响应:
          response.writeHead(404);
          response.end('404 Not Found');
        }
      });
    });

  }*/

  isExit(userName: string, projectName: string): Observable<string[]> {
    const url = `http://47.52.116.116:8075/file/projectExit?userName=${userName}&projectName=${projectName}`;
    return this.http.get<string[]>(url);
  }

  searchProject(userName: string): Observable<string[]> {
    const url = `http://47.52.116.116:8075/file/searchProject?userName=${userName}`;
    return this.http.get<string[]>(url);
  }

  searchVersion(userName: string, projectName: string): Observable<string[]> {
    const url = `http://47.52.116.116:8075/file/searchVersion?userName=${userName}&projectName=${projectName}`;
    return this.http.get<string[]>(url);
  }

  getSafeNL(userName: string, projectName: string, version: string): Observable<string[]> {
    const url = `http://47.52.116.116:8075/file/getSafeNL?userName=${userName}&projectName=${projectName}&version=${version}`;
    return this.http.get<string[]>(url);
  }
}
